package com.lz.sports.vm.group

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.lz.library.base.BaseModel
import com.lz.library.base.BaseViewModel
import com.lz.sports.bean.DeviceInfo
import com.lz.ub.UBCCallback
import com.lz.ub.UBCDevice
import com.lz.ub.UBCManager

class AssistGroupViewModel : BaseViewModel<BaseModel>(), UBCCallback {

    val deviceInfoList = MutableLiveData<List<DeviceInfo>>()

    override fun onCreate(owner: LifecycleOwner) {
        UBCManager.get().addUBCCallback(this)
    }

    override fun onCleared() {
        super.onCleared()
        UBCManager.get().removeUBCCallback(this)
    }

    fun connect() {

    }

    override fun onReadHex(device: UBCDevice, bytes: ByteArray, start: Int, size: Int, isAssist: Boolean) {
    }

    override fun onRead(device: UBCDevice, data: String, isAssist: Boolean) {
    }

    override fun onConnected(device: UBCDevice, isAssist: Boolean) {
        if (deviceInfoList.value == null) return
        val list = deviceInfoList.value!!
        for (deviceInfo in list) {
            if (device.mac == deviceInfo.mac) {
                deviceInfo.changeLine(true)
            }
        }
    }

    override fun onDisconnected(device: UBCDevice, isAssist: Boolean) {
        if (deviceInfoList.value == null) return
        val list = deviceInfoList.value!!
        for (deviceInfo in list) {
            if (device.mac == deviceInfo.mac) {
                deviceInfo.changeLine(false)
            }
        }
    }

}